package managerBeanImpl;

import java.util.List;

import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import managerBean.UtenteManagerBean;

@Stateless
public class UtenteManagerBeanImpl implements UtenteManagerBean{

	@PersistenceContext
    private EntityManager em;
	
	@Override
	public List visualizzaUtentiMgr() {
		String username=FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
		Query query=em.createQuery("SELECT u FROM UtenteRegistrato u");
		if (username != null)
			query=em.createQuery("SELECT u FROM UtenteRegistrato u WHERE u.username <> '"+username+"'");
		return query.getResultList();
	}

	@Override
	public List cercaUtentiMgr(String cerca) {
		String username=FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
		Query query=em.createQuery("SELECT u FROM UtenteRegistrato u WHERE u.username='"+cerca+"'");
		if (username != null)
			query=em.createQuery("SELECT u FROM UtenteRegistrato u WHERE u.username <> '"+username+"' and u.username='"+cerca+"'");
		return query.getResultList();
	}

	@Override
	public List visualizzaWishlistMgr(int idWishlist) {
		Query query=em.createQuery("SELECT p FROM Pacchetto p,Wishlist w WHERE w.id.idWishlist="+idWishlist+" and w.id.idPacchetto=p.idPacchetto");
		return query.getResultList();
	}

}
